package com.chezai.client.utils;

import android.text.TextUtils;

import com.chezai.client.dao.manager.ClassRecordDaoManager;
import com.chezai.client.dao.manager.PeriodRecordDaoManager;
import com.chezai.client.dao.manager.TerminalBaseInfoDaoManager;
import com.chezai.client.global.Const;
import com.orhanobut.logger.Logger;

/**
 * Describe 构建数据包工具类
 * Created by LZF on 2016/8/29.
 */
public class BuildDataUtil {
    private String mPart;
    private String mProject;
    private String mDrivingType;

    /**
     * 生成课堂id
     */
    public static long buildClassId() {
        long classId = ClassRecordDaoManager.getInstance().findLastClassId();
        String date = DateUtil.timestampStr("yyMM");
        if (classId == 0) {
            return Long.valueOf(date + String.format("%04d", classId + 1));
        }
        return classId + 1;
    }

    /**
     * 获取教练的ClassId
     */
    public static long getCoachClassId() {
        long classId = PreferenceManager.getLong(Const.SP_COACH_CLASS_ID, 0);
        if (classId == 0) {
            classId = Long.parseLong(DateUtil.timestampStr(DateUtil.sf20));
        }
        PreferenceManager.putLong(Const.SP_COACH_CLASS_ID, classId);
        return classId;
    }

    /**
     * 清空教练的ClassId
     */
    public static void clearCoachClassId() {
        PreferenceManager.putLong(Const.SP_COACH_CLASS_ID, 0);
    }

    /**
     * 获取学员的ClassId
     */
    public static long getStudentClassId() {
        long classId = PreferenceManager.getLong(Const.SP_STUDENT_CLASS_ID, 0);
        if (classId == 0) {
            classId = Long.parseLong(DateUtil.timestampStr(DateUtil.sf20));
        }
        setStudentClassId(classId);
        return classId;
    }

    /**
     * 设置学员课程iD
     *
     * @param classId
     */
    public static void setStudentClassId(long classId) {
        PreferenceManager.putLong(Const.SP_STUDENT_CLASS_ID, classId);
    }

    /**
     * 清空学员课程ID
     */
    public static void clearStudentClassId() {
        PreferenceManager.putLong(Const.SP_STUDENT_CLASS_ID, 0);
    }

    /**
     * 培训部分码定义
     *
     * @param part 1-第一部分，2-第二部分，3-第三部分，4-第四部分
     */
    public void setPart(String part) {
        this.mPart = part;
    }

    /**
     * 培训项目码
     *
     * @param project
     */
    public void setProject(String project) {
        this.mProject = project;
    }

    /**
     * 学员准驾类型
     *
     * @param drivingType
     */
    public void setStudentDrivingType(String drivingType) {
        this.mDrivingType = drivingType;
    }

    /**
     * 生成A.4.2　课程编码 具体看协议文档
     *
     * @return
     */
    public String buildCourseId() {
        StringBuilder sb = new StringBuilder();
        sb.append(TerminalBaseInfoDaoManager.getInstance().getTerminalType());
        String result = "";
        if (TextUtils.isEmpty(mDrivingType)) {
            result = "00";
        } else if (mDrivingType.contains("A")) {
            result = mDrivingType.replace("A", "0");
        } else if (mDrivingType.contains("B")) {
            result = mDrivingType.replace("B", "1");
        } else if (mDrivingType.contains("C")) {
            result = mDrivingType.replace("C", "2");
        } else if (TextUtils.equals(mDrivingType, "D")) {
            result = "31";
        } else if (TextUtils.equals(mDrivingType, "E")) {
            result = "32";
        } else if (TextUtils.equals(mDrivingType, "F")) {
            result = "33";
        } else if (TextUtils.equals(mDrivingType, "M")) {
            result = "41";
        } else if (TextUtils.equals(mDrivingType, "N")) {
            result = "42";
        } else if (TextUtils.equals(mDrivingType, "P")) {
            result = "43";
        }
        sb.append(result);
        sb.append(mPart);
        sb.append(mProject);
        int length = sb.length();
        for (int i = 0; i < 10 - length; i++) {
            sb.append("0");
        }
        String courseNum = sb.toString();
        PreferenceManager.putString(Const.SP_CURRENT_COURSE_NUM, courseNum);
        return courseNum;
    }

    /**
     * A.4.1
     * 生成学时记录编号
     *
     * @return String[]
     * 0位是 sequence
     * 1位是 完整学时记录编号
     */
    public String[] periodRecord() {
        String[] result = new String[2];
        // 计时终端编号
        String device = TerminalBaseInfoDaoManager.getInstance().getTerminalDevice();
        String date = DateUtil.timestampStr("yyMMdd");
        int sequence = PeriodRecordDaoManager.getInstance().getSequence(date);
        Logger.t("BuildDataUtil sequence").d("sequence: " + sequence);
        String sequenceStr = String.format("%04d", sequence + 1);//string str = string.format("%4d", 12); 结果："  12"
        result[0] = String.valueOf(sequenceStr);
        result[1] = device + date + sequenceStr;
        return result;
    }

}
